local joy__youlong = fk.CreateSkill {

  name = "joy__youlong",

  tags = {  },

}



joy__youlong:addEffect("viewas", {
  name = "joy__youlong",
  switch_skill_name = "joy__youlong",
  anim_type = "switch",
  pattern = ".",
  prompt = function ()
    return "#joy__youlong-prompt"..player:getSwitchSkillState("joy__youlong")
  end,
  interaction = function(self,player)
    local names = {}
    local mark = player:getTableMark( "@$joy__youlong")
    local isYang = player:getSwitchSkillState("joy__youlong") == fk.SwitchYang
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if ((card.type == Card.TypeBasic and not isYang) or
        (card:isCommonTrick() and isYang)) and
        not card.is_derived and
        ((Fk.currentResponsePattern == nil and player:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
        if not table.contains(mark, card.trueName) then
          table.insertIfNeed(names, card.name)
        end
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = function(self, player, to_select, selected)
    return player:getSwitchSkillState("joy__youlong") == fk.SwitchYin and #selected == 0
    and Fk:getCardById(to_select).type ~= Card.TypeBasic and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  view_as = function(self, player,cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    if player:getSwitchSkillState("joy__youlong") == fk.SwitchYin then
      if #cards ~= 1 then return end
      card:setMark("joy__youlong_card", cards[1])
    end
    card.skillName = joy__youlong.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local mark = player:getTableMark( "@$joy__youlong")
    table.insert(mark, use.card.trueName)
    room:setPlayerMark(player, "@$joy__youlong", mark)
    local state = player:getSwitchSkillState(joy__youlong.name, true, true)
    room:setPlayerMark(player, "joy__youlong_" .. state .. "-turn", 1)
    if state == "yin" then
      local id = use.card:getMark("joy__youlong_card")
      if id ~= 0 then
        room:throwCard(id, joy__youlong.name, player, player)
      end
    else
      local choices = player:getAvailableEquipSlots()
      if #choices == 0 then return end
      local choice = room:askForChoice(player, choices, joy__youlong.name, "#joy__youlong-choice", false, player.equipSlots)
      room:abortPlayerArea(player, choice)
    end
  end,
  enabled_at_play = function(self, player)
    local state = player:getSwitchSkillState(joy__youlong.name, false, true)
    if player:getMark("joy__youlong_" .. state .. "-turn") == 0 or player:getMark("joy__youlong_levelup") > 0 then
      return state == "yin" or#player:getAvailableEquipSlots() > 0
    end
  end,
  enabled_at_response = function(self, player, response)
    if response then return end
    local state = player:getSwitchSkillState(joy__youlong.name, false, true)
    local pat = state == "yin" and "basic" or "trick"
    if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):matchExp(".|0|nosuit|none|.|"..pat) then
      if player:getMark("joy__youlong_" .. state .. "-turn") == 0 or player:getMark("joy__youlong_levelup") > 0 then
        return state == "yin" or#player:getAvailableEquipSlots() > 0
      end
    end
  end,
})

return joy__youlong